package com.rong.text;

import com.rong.dao.zhMapper;
import com.rong.pojo.zh;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class test02 {
    public static void main(String[] args) {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory =
                    new SqlSessionFactoryBuilder().build(inputStream);
            //           核心类，执行增删改查操作的，类似于jdbc中的satetment
            SqlSession session= sqlSessionFactory.openSession();
            zhMapper mapper=session.getMapper(zhMapper.class);
            zh user = new zh();
            user.setName("东方不败");
            user.setId((long)4 );
            user.setUserName("dongfangbubai");
//           调用新增方法，将输入添加到数据库中
            int num=  mapper.insertSelective(user);
            System.out.println(num);
            zh user1 = new zh();
            user1.setName("叶轻眉");
            user1.setId((long)3 );
            user1.setUserName("yqm");
            //           调用修改方法，将第三条数据修改
            int upd=  mapper.updateByPrimaryKeySelective(user1);
            System.out.println(upd);
            //            手动提交事务
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

